﻿using System;
using System.Collections.Generic;
using System.Linq;
using FW.Core.Model;
using FW.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FW.Test
{
    [TestClass]
    public class RepoTest
    {
        [TestMethod]
        public void TestGetAll()
        {
            var repo = new Repository<Film>(new DbContextFactory());
            var result = repo.GetAll();

            Assert.AreNotEqual(result.Count(), 0);
        }

        [TestInitialize]
        public void Init()
        {
            using (var db = new DbContextFactory(false))
            {
                var repoActor = new Repository<Actor>(db);

                var actor = new Actor() { Name = "A 01" };

                repoActor.Insert(actor);

                var repo = new Repository<Film>(db);
                repo.Insert(new Film() { Title = "F 01", PublishDate = DateTime.Now });
                repo.Insert(new Film() { Title = "F 02", PublishDate = DateTime.Now });
                repo.Insert(new Film() { Title = "F 03", PublishDate = DateTime.Now });
                repo.Insert(new Film() { Title = "F 04", PublishDate = DateTime.Now });
                repo.Insert(new Film() { Title = "F 05", PublishDate = DateTime.Now });

                repo.SaveChanges();

                db.TranComplete();
            }
        }

        [TestCleanup]
        public void Cleanup()
        {
            using (var db = new DbContextFactory(false))
            {
                var repo = new Repository<Film>(db);

                foreach (var film in repo.GetAll())
                {
                    repo.Delete(film);
                }

                var repoActors = new Repository<Actor>(db);
                foreach (var actor in repoActors.GetAll())
                {
                    repoActors.Delete(actor);
                }

                repo.SaveChanges();
                db.TranComplete();
            }
        }
    }
}
